package com.zjz.demo.lock;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/10/10/12:56
 * @Description:
 * @Target: 死锁,锁里有锁
 */
public class DeadLock {


    public static void main(String[] args) {

        String lockA = "A";
        String lockB = "B";
        MyThread myThread1 = new MyThread(lockA, lockB);
        MyThread myThread2 = new MyThread(lockB, lockA);


        new Thread(myThread1,"T1").start();
        new Thread(myThread2,"T2").start();

    }

}




class MyThread  implements Runnable{

    private String lockA;
    private String lockB;

    public MyThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }


    @Override
    public void run() {

        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+lockA+"---->get"+lockB);
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+lockB+"----->get"+lockA);
            }
        }
    }
}
